<?php
namespace app\beautiful\controller\admin\setting;

use app\BaseController;
use app\common\model\ShopModel;
use app\common\model\ShopDetailModel;
use app\common\model\ShopStoreModel;
use app\common\logic\DataLogic;

class Store extends BaseController{
    protected $name='门店';
    
    public function getStoreList()
    {
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        $store = new ShopStoreModel();
        $order=['is_main'=>'asc','add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($store,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        if(!empty($return['list'])) {
            foreach ($return['list'] as &$value) {
                $value['enable_text'] = $value['enable'] == 1 ? '启用' : '禁用';
            }
        }
        // 是否还可以创建门店
        $return['count'] = $this->request->shop->number;
        $return['create'] = 1; // 可以创建
        if($return['total'] >= $this->request->shop->number) {
            $return['create'] = 0; // 不可以创建
        }
        return $this->success($return);

    }
    
    public function showStore() {
        $id = $this->request->param('id/d',0);
        if($id == 0){
            $shop_store = new ShopStoreModel();
        }else{
            $shop_store = ShopStoreModel::find($id);
        }
        $detail=[
            'id'=>!empty($shop_store->id) ? $shop_store->id : 0,
            'name'=>!empty($shop_store->name) ? $shop_store->name : '',
            'logo'=>!empty($shop_store->logo) ? $shop_store->logo : '',
            'door_license_plate'=>!empty($shop_store->door_license_plate) ? $shop_store->door_license_plate : '',
            'business_hours'=>!empty($shop_store->business_hours) ? $shop_store->business_hours : '',
            'tel'=>!empty($shop_store->tel) ? $shop_store->tel : '',
            'address'=>!empty($shop_store->address) ? $shop_store->address : '',
            'info'=>!empty($shop_store->info) ? $shop_store->info : '',
            'lng'=>!empty($shop_store->lng) ? $shop_store->lng : '',
            'lat'=>!empty($shop_store->lat) ? $shop_store->lat : '',
            'is_main'=>!empty($shop_store->is_main) ? $shop_store->is_main : 2,
        ];
        return $this->success(['detail'=>$detail]);
    }
    
    public function saveStore() {
        $id = $this->request->param('id/d',0);
        $name=$this->request->param('name');
        $logo=$this->request->param('logo');
        $door_license_plate=$this->request->param('door_license_plate');
        $business_hours=$this->request->param('business_hours');
        $tel=$this->request->param('tel');
        $address=$this->request->param('address');
        $info=$this->request->param('info');
        $lng=$this->request->param('lng');
        $lat=$this->request->param('lat');
        $is_main=$this->request->param('is_main');
        if(empty($name)){
            return $this->error('名称必须填写');
        }
        if(empty($logo)){
            return $this->error('请上传logo');
        }
        if(empty($door_license_plate)){
            return $this->error('请上传门店门牌照');
        }
        if(empty($business_hours)){
            return $this->error('请填写营业时间');
        }
        if(empty($tel)){
            return $this->error('请填写门店电话');
        }
        if(empty($address)){
            return $this->error('请填写门店地址');
        }
        if(empty($info)){
            return $this->error('请填写门店说明');
        }
        if(empty($lng) || empty($lat)){
            return $this->error('请选择门店定位');
        }
        if($id == 0){
            $shop_store = new ShopStoreModel();
             $shop_store->shop_id=$this->request->shopId;
        }else{
            $shop_store = ShopStoreModel::find($id);
        }
        $pre_logo=$shop_store->logo;
        $pre_door_license_plate=$shop_store->door_license_plate;
        $shop_store->logo=$logo;
        $shop_store->name=$name;
        $shop_store->door_license_plate=$door_license_plate;
        $shop_store->business_hours=$business_hours;
        $shop_store->tel=$tel;
        $shop_store->address=$address;
        $shop_store->info=$info;
        $shop_store->lng=$lng;
        $shop_store->lat=$lat;
        $shop_store->save();
        // 修改总部门店更新商户详情及名称
        if($is_main == 1) {
            $this->request->shop->name=$name;
            $this->request->shop->save();
            $shop_detail = ShopDetailModel::find($this->request->shopId);
            $shop_detail->logo=$logo;
            $shop_detail->door_license_plate=$door_license_plate;
            $shop_detail->business_hours=$business_hours;
            $shop_detail->tel=$tel;
            $shop_detail->address=$address;
            $shop_detail->info=$info;
            $shop_detail->lng=$lng;
            $shop_detail->lat=$lat;
            $shop_detail->save();
        }
        imgReplaceAct($logo,$pre_logo);
        imgReplaceAct($door_license_plate,$pre_door_license_plate);
        return $this->success('保存成功');
    }
    // 启用、禁用
    public function enableStore() {
        $id     = $this->request->param('id/d',0);
        $enable = $this->request->param('enable/d',0);
        if(null === $shop_store = ShopStoreModel::find($id)){
            return $this->error('删除门店不存在');
        }
        if($shop_store->is_delete != 0) {
            return $this->error('门店已删除');
        }
        if($enable == $shop_store->enable) {
            return $this->error('状态错误');
        }
        // 当前启用的数据和设置对比
        $count = $this->request->shop->number;
        // 启用的门店
        $store_count = ShopStoreModel::where([
            ['shop_id','=',$this->request->shopId],
            ['enable','=',1]
        ])->count();
        if(($count - $store_count) < 1 && $enable == 1) {
            return $this->error('当前共可以启用'.$count.'个门店');
        }
        $shop_store->enable = $enable;
        $shop_store->save();
        return $this->success('设置成功');
    }
    
    // 删除
    public function deleteStore() {
        $id = $this->request->param('id/d',0);
        if(null === $shop_store = ShopStoreModel::find($id)){
            return $this->error('删除门店不存在');
        }
        if($shop_store->is_delete != 0) {
            return $this->error('门店已删除');
        }
        $shop_store->is_delete = 1;
        $shop_store->save();
        return $this->success('删除成功');
    }
    
    public function getSelectStoreList() {
        $where = [
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['enable','=',1],
        ];
        $store = new ShopStoreModel();
        $order=['is_main'=>'asc','add_time'=>'desc'];
        $field='*';
        $append=['add_time_format'];
        $return = DataLogic::getDataList($store,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }
}